package com.ln.property_control.service.impl;

import com.ln.property_control.mapper.UserMapper;
import com.ln.property_control.pojo.ResultVo;
import com.ln.property_control.pojo.User;
import com.ln.property_control.service.UserService;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;

import static java.lang.Math.ceil;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    public ResultVo  queryUser(User user,HttpSession session) {

        ResultVo resultVo = new ResultVo();
//        用户名即密码
        User user1 = userMapper.queryUser(user);
        if(user1==null){
//            System.out.println("账号密码错误");
            resultVo.setCode(1);
            resultVo.setMsg("账号密码错误");
        }
//        用户名
        User user2 = userMapper.queryUserName(user);

        if(user2 != null){
            Integer error_num = user2.getFailure_num(); //错误次数
            int login_date = Integer.parseInt(user2.getLogin_date());//错误时间
            int uid = user2.getUid(); //用户id
            int current_time = (int) (DateTime.now().getMillis() / 1000); //当前时间戳
            if(user.getPassword().equals(user2.getPassword())){ //密码正确
//                判断错误次数大于等于3（已锁定） && 错误时间在20分钟之内
                if(error_num >= 3 && current_time - login_date <= 1200){
                    int ceil = (int)(20 - (Math.ceil((double) (current_time - login_date) / 60)));
//                    System.out.println("用户已被锁定，请在"+ceil+"分钟后重试");
                    resultVo.setCode(1);
                    resultVo.setMsg("用户已被锁定，请在"+ceil+"分钟后重试");
                }else {
                     session.setAttribute("userInfo",user1);
                     session.setMaxInactiveInterval(600);
                    resultVo.setCode(0);
                    resultVo.setMsg("登陆成功");
                }
            } else {
                if(current_time - Integer.parseInt(user2.getLogin_date()) >= 1200){
                    User user3 = new User();
                    user3.setFailure_num(1);
                    user3.setLogin_date(current_time+"");
                    user3.setUid(user2.getUid());
                    userMapper.getUpdateUser(user3);
//                    System.out.println("密码错误,还有两次机会");
                    resultVo.setCode(1);
                    resultVo.setMsg("密码错误,还有两次机会");
                }else if(error_num>=3){
                    int  ceil = (int) (20 - (Math.ceil((double) (current_time - login_date) / 60)));
//                    System.out.println("用户已被锁定，请在"+ceil+"分钟后重试");
                    resultVo.setCode(1);
                    resultVo.setMsg("用户已被锁定，请"+ceil+"分钟后重试");
               } else {
                    User user3 = new User();
                    user3.setFailure_num(error_num+1);
                    user3.setLogin_date(current_time+"");
                    user3.setUid(user2.getUid());
                    int updateUser = userMapper.getUpdateUser(user3);
                    if ((3-(error_num+1)) == 0){
                        resultVo.setCode(1);
                        resultVo.setMsg("用户已被锁定，请19分钟后重试");

                    }else {
//                    System.out.println("密码错误，还有"+(3-(error_num+1))+"机会");
                        resultVo.setCode(1);
                        resultVo.setMsg("密码错误，还有" + (3 - (error_num + 1)) + "机会");
                    }
                }
            }
        }else {
//            System.out.println("账号密码错误");
            resultVo.setCode(1);
            resultVo.setMsg("账号密码错误");
        }
        return resultVo;
    }

}
